package com.isoftstone.bank.service.impl;


import com.isoftstone.bank.service.IMerchantService;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.common.entity.SysUser;
import com.isoftstone.merchant.dao.city.CityMapper;
import com.isoftstone.merchant.dao.merchant.MerchantMapper;
import com.isoftstone.merchant.entity.city.City;
import com.isoftstone.merchant.entity.mdept.MDept;
import com.isoftstone.merchant.entity.merchant.Merchant;
import com.isoftstone.merchant.service.mdept.IMdeptService;
import com.isoftstone.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by wlj on 2017/11/29.
 */
@Service
public class MerchantService implements IMerchantService{

    @Autowired
    MerchantMapper merchantMapper;

    @Autowired
    IMdeptService mdeptService;

    @Autowired
    CityMapper cityMapper;

    @Override
    public List<Merchant> getMerchantlistPage(Page page){

        if(null != page.getPd().get("orgIds")){
            page.getPd().put("orgIds", Arrays.asList(page.getPd().get("orgIds").toString().split(",")));
        }
        return this.merchantMapper.getMerchantlistPage(page);
    }

    @Override
    public int disableMerchant(String corpId,PageData pd) {
        pd.put("corpId",corpId);
        if(null == pd.get("status")){
            pd.put("status","1");
        }
        return this.merchantMapper.disableMerchant(pd);
    }

    @Override
    public Tidings<String> updateMerchant(Merchant merchant, SysUser user, String remoteAddr) throws Exception {
        Tidings<String> tidings = new Tidings<String>();

        if(null != merchant){
            if(XSSCheckUtil.checkXSS( merchant.getCorpId())
                    || XSSCheckUtil.checkXSS( merchant.getCorpCode())
                    || XSSCheckUtil.checkXSS( merchant.getCorpName())
                    || XSSCheckUtil.checkXSS( merchant.getAddr())
                    || XSSCheckUtil.checkXSS( merchant.getCardNo())
                    || XSSCheckUtil.checkXSS( merchant.getCityId())
                    || XSSCheckUtil.checkXSS( merchant.getContractFile())
                    || XSSCheckUtil.checkXSS( merchant.getCorpDesc())
                    || XSSCheckUtil.checkXSS( merchant.getAddr())
                    || XSSCheckUtil.checkXSS( merchant.getLinkMan())
                    || XSSCheckUtil.checkXSS( merchant.getLinkPhone())
                    || XSSCheckUtil.checkXSS( merchant.getOrgId())
                    || XSSCheckUtil.checkXSS( merchant.getRecommend())
                    || XSSCheckUtil.checkXSS( merchant.getStatus())
                    || XSSCheckUtil.checkXSS( merchant.getLinkEmail())){

                tidings.setMsg("非法参数");
                tidings.setStatus(Const.ERROR);
            }
            merchant.setUpdateIp(remoteAddr);
            merchant.setUpdateDate(new Date());
            merchant.setUpdator(user.getUserid());

            int result = this.merchantMapper.updateByCorpId(merchant);
            if (result == 1 ){
                tidings.setMsg("更新成功！");
                tidings.setStatus(Const.SUCCESS);
            }else{
                tidings.setMsg("更新失败！");
                tidings.setStatus(Const.ERROR);
            }
        }
        return tidings;
    }
public static void main(String [] args){
    System.out.println(DateUtil.fomatDateTime(DateUtil.getTime()));
    System.out.println(DateUtil.getTime());
    System.out.println(DateUtil.fomatDateTime(DateUtil.getTime()));
}
    @Override
    public Tidings<String> addMerchant(Merchant merchant, SysUser user, String remoteAddr) throws Exception {
        Tidings<String> tidings = new Tidings<>();
        merchant.setUpdateIp(remoteAddr);
        merchant.setCreateDate(new Date());
        merchant.setUpdateDate(new Date());
        merchant.setCreator(user.getUserid());
        merchant.setUpdator(user.getUserid());

        merchant.setCorpCode(getMerchantCorpCode(merchant));

        int result = this.merchantMapper.insert(merchant);

        tidings.setStatus(Const.SUCCESS);
        if( result == 1 ){
            MDept dept=new MDept();
            dept.setDeptName(merchant.getCorpName());//商户名称
            dept.setSuperDeptId("0");//上级部门id
            dept.setLevel(1);//级别
            dept.setOrderNo(1);//排序号
            dept.setCreateDate(new Date());//创建时间
            dept.setCreator(user.getNickname());//创建人
            dept.setDeptId(UuidUtil.getNumFormatUUID());//部门id
            dept.setCorpId(user.getCorpId());//商户编号
            try{
//                mdeptService.insertFirstdept(dept);//mark 暂时关闭
                tidings.setMsg("新增商户成功");
            }catch (Exception e){
                tidings.setMsg("商户新增成功，部门新增失败");
            }
        }else{
            tidings.setMsg("新增商户失败");
        }
        return tidings;
    }

    /**
     * 生成商户编码
     * @param merchant
     * @return
     */
    private String getMerchantCorpCode(Merchant merchant){

        City city = this.cityMapper.selectByPrimaryKey(merchant.getCityId());

        Page page = new Page();
        page.setShowCount(9999999);
        PageData pd = new PageData();
        pd.put("cityId",merchant.getCityId());
        page.setPd(pd);
        List<Merchant> list = this.merchantMapper.getMerchantlistPage(page);
        int count = 0;
        if(null != list && list.size()>0){
            String temCode = list.get(0).getCorpCode();
            String lastThree = temCode.substring(temCode.length()-3,temCode.length());
            count = Integer.parseInt(lastThree);
        }

        return Const.MERCHANT_CODE_FRONT + city.getDistrictCode() + String.format("%03d",(count+1));
    }

    @Override
    public Tidings<Merchant> getMerchantById(String corpId) {
        Tidings<Merchant> tidings = new Tidings<Merchant>();
        Merchant merchant = this.merchantMapper.selectByPrimaryKey(corpId);
        tidings.setStatus(Const.SUCCESS);
        tidings.setT(merchant);
        if(null != merchant){
            tidings.setMsg("查询成功");
        }else{
            tidings.setMsg("查询结果为空");
        }
         return tidings;
    }
    @Override
    public Tidings<List<Merchant>>  getAllMerchantList(PageData pd) throws Exception {
        Tidings<List<Merchant>> tidings = new Tidings<>();

        if(null != pd.get("orgIds")){
            pd.put("orgIds", Arrays.asList(pd.get("orgIds").toString().split(",")));
        }

        tidings.setT(this.merchantMapper.getMerchantList(pd));
        tidings.setStatus(Const.SUCCESS);

        return tidings;
    }

}
